<template>
  <Header :title="{ zh: '成型目视管理看板', en: 'Assembly Visual Board', index: 6 }" />

  <div class="EchartsContainer">
    <swiper
      :autoplay="{
        delay: 60000
      }"
      :slides-per-view="1"
      :space-between="2000"
      style="height: 50%"
    >
      <swiper-slide>
        <div class="row" style="width: 100%; height: 100%">
          <div class="item">
            <div class="title">成一</div>
            <div class="echarts" id="main"></div>
          </div>
          <div class="item">
            <div class="title">成二</div>
            <div class="echarts" id="main2"></div>
          </div>
        </div>
      </swiper-slide>
      <swiper-slide>
        <div class="row" style="width: 100%; height: 100%">
          <div class="item">
            <div class="title">成三</div>
            <div class="echarts" id="main3"></div>
          </div>
          <div class="item" style="padding-right: 20px">
            <div class="title">成四</div>
            <div class="echarts" id="main4"></div>
          </div>
        </div>
      </swiper-slide>
      <swiper-slide>
        <div class="row" style="width: 100%; height: 100%">
          <div class="item">
            <div class="title">成五</div>
            <div class="echarts" id="main51"></div>
          </div>
<!--          <div class="item" style="padding-right: 20px">-->
<!--            <div class="title">外协成型</div>-->
<!--            <div class="echarts" id="mainw"></div>-->
<!--          </div>-->
        </div>
      </swiper-slide>
    </swiper>

    <div class="row" style="margin-top: 10px">
      <div class="item" style="width: 50%">
        <div class="title">面底配套<span style="color: red">(双)</span></div>
        <div class="echarts" id="main5"></div>
      </div>
      <div class="item" style="margin-left: 10px">
        <div class="title" style="text-align: start">面底库存明细</div>
        <el-table
          ref="tab"
          border
          class="table"
          :data="tableData"
          style="width: 100%; height: 100%"
        >
          <el-table-column align="center" prop="custom" label="品牌" width="70" />
          <el-table-column align="center" prop="lotNo" label="指令号" />
          <el-table-column align="center" prop="orderQty" label="订单数量" />
          <el-table-column align="center" prop="shortQty" label="欠数" />
          <el-table-column align="center" prop="stkQty" label="库存数" />
          <el-table-column align="center" prop="stkDet" label="库存明细" width="200" />
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { onBeforeUnmount } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'

let rollTimer = ref() as any
const tab = ref<HTMLElement>() as any
const autoRoll = (stop: any) => {
  if (stop) {
    clearInterval(rollTimer.value)
    return
  }
  const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  rollTimer.value = setInterval(() => {
    divData.scrollTop += 1
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
  }, 150)
}
onMounted(() => {
  autoRoll(false)

})
onBeforeUnmount(() => {
  autoRoll(true)
})

const showData = ref([
  //成一
  {
    data: [
      {
        name: '在制品',
        type: 'bar',
        data: [1700],
        color: '#FAC858',
        barWidth: '10%',
        barGap: '100%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '入库',
        type: 'bar',
        data: [1600],
        color: '#93BEFF',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '产出',
        type: 'bar',
        data: [1758],
        color: '#72DDB3',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '投入',
        type: 'bar',
        color: '#FF9940',
        data: [2011],
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      }
    ]
  },
  //成二
  {
    data: [
      {
        name: '在制品',
        type: 'bar',
        data: [1200],
        color: '#FAC858',
        barWidth: '10%',
        barGap: '100%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '入库',
        type: 'bar',
        data: [600],
        color: '#93BEFF',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '产出',
        type: 'bar',
        data: [700],
        color: '#72DDB3',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '投入',
        type: 'bar',
        color: '#FF9940',
        data: [725],
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      }
    ]
  },
  //成三
  {
    data: [
      {
        name: '在制品',
        type: 'bar',
        data: [1500],
        color: '#FAC858',
        barWidth: '10%',
        barGap: '100%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '入库',
        type: 'bar',
        data: [1600],
        color: '#93BEFF',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '产出',
        type: 'bar',
        data: [1800],
        color: '#72DDB3',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '投入',
        type: 'bar',
        color: '#FF9940',
        data: [2004],
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      }
    ]
  },
  //成四
  {
    data: [
      {
        name: '在制品',
        type: 'bar',
        data: [1450],
        color: '#FAC858',
        barWidth: '10%',
        barGap: '100%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '入库',
        type: 'bar',
        data: [1500],
        color: '#93BEFF',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '产出',
        type: 'bar',
        data: [1850],
        color: '#72DDB3',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '投入',
        type: 'bar',
        color: '#FF9940',
        data: [2020],
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      }
    ]
  },
  //成5
  {
    data: [
      {
        name: '在制品',
        type: 'bar',
        data: [1450],
        color: '#FAC858',
        barWidth: '10%',
        barGap: '100%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '入库',
        type: 'bar',
        data: [1500],
        color: '#93BEFF',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '产出',
        type: 'bar',
        data: [1850],
        color: '#72DDB3',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '投入',
        type: 'bar',
        color: '#FF9940',
        data: [2020],
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      }
    ]
  },
  //外协成型
  {
    data: [
      {
        name: '在制品',
        type: 'bar',
        data: [1450],
        color: '#FAC858',
        barWidth: '10%',
        barGap: '100%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '入库',
        type: 'bar',
        data: [1500],
        color: '#93BEFF',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      /*
      {
        name: '产出',
        type: 'bar',
        data: [1850],
        color: '#72DDB3',
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      },
      {
        name: '投入',
        type: 'bar',
        color: '#FF9940',
        data: [2020],
        barWidth: '10%',
        label: {
          show: true,
          position: 'right'
        }
      }
      */
    ]
  }
]) as any
const chart2 = ref([]) as any
const DeptNos = ref([]) as any
const DeptName = ref([]) as any
const getMDData = ref([]) as any
const getDeptTypes = async () => {
  try {
    const res = (await request.post('/last/show/stk/status')) as any
    res.forEach((item: any) => {
      if (item.type === '配套库存') getMDData.value.push(item.count)
      if (item.type === '大底库存') getMDData.value.push(item.count)
      if (item.type === '鞋面库存') getMDData.value.push(item.count)
    })
  } catch (error: any) {
    ElMessage.error(error)
  }
}
const getData = async () => {
  // 获取面底库存明细
  try {
    let tableDataList = (await request.post<any, any>(`/last/show/stk/detail`, {
      pageNum: '1',
      pageSize: '200'
    })) as any
    tableData.value = tableDataList.list
  } catch (error: any) {
    ElMessage.error(error)
  }
}

const getData2 = async () => {
  try {
    let tableDataList = (await request.post<any, any>(`/last/show/dept/status`)) as any
    const cheng1 = tableDataList['成1']
    showData.value[0].data[0].data[0] = cheng1.find((x: any) => x.title === "在制品")?.count || 0;
    showData.value[0].data[1].data[0] = cheng1.find((x: any) => x.title === "入库")?.count || 0;
    showData.value[0].data[2].data[0] = cheng1.find((x: any) => x.title === "产出")?.count || 0;
    showData.value[0].data[3].data[0] =cheng1.find((x: any) => x.title === "投产")?.count || 0;
    const cheng2 = tableDataList['成2']
    showData.value[1].data[0].data[0] = cheng2.find((x: any) => x.title === "在制品")?.count || 0;
    showData.value[1].data[1].data[0] = cheng2.find((x: any) => x.title === "入库")?.count || 0;
    showData.value[1].data[2].data[0] = cheng2.find((x: any) => x.title === "产出")?.count || 0;
    showData.value[1].data[3].data[0] = cheng2.find((x: any) => x.title === "投产")?.count || 0;
    const cheng3 = tableDataList['成3']
    showData.value[2].data[0].data[0] = cheng3.find((x: any) => x.title === "在制品")?.count || 0;
    showData.value[2].data[1].data[0] = cheng3.find((x: any) => x.title === "入库")?.count || 0;
    showData.value[2].data[2].data[0] = cheng3.find((x: any) => x.title === "产出")?.count || 0;
    showData.value[2].data[3].data[0] = cheng3.find((x: any) => x.title === "投产")?.count || 0;
    const cheng4 = tableDataList['成4']
    showData.value[3].data[0].data[0] = cheng4.find((x: any) => x.title === "在制品")?.count || 0;
    showData.value[3].data[1].data[0] = cheng4.find((x: any) => x.title === "入库")?.count || 0;
    showData.value[3].data[2].data[0] = cheng4.find((x: any) => x.title === "产出")?.count || 0;
    showData.value[3].data[3].data[0] = cheng4.find((x: any) => x.title === "投产")?.count || 0;
    const cheng5 = tableDataList['成5']
    showData.value[4].data[0].data[0] = cheng5.find((x: any) => x.title === "在制品")?.count || 0;
    showData.value[4].data[1].data[0] = cheng5.find((x: any) => x.title === "入库")?.count || 0;
    showData.value[4].data[2].data[0] = cheng5.find((x: any) => x.title === "产出")?.count || 0;
    showData.value[4].data[3].data[0] = cheng5.find((x: any) => x.title === "投产")?.count || 0;
   /* const cheng6 = tableDataList['外协成型']
    showData.value[5].data[0].data[0] = cheng6.find((x: any) => x.title === "在制品")?.count || 0;
    showData.value[5].data[1].data[0] = cheng6.find((x: any) => x.title === "入库")?.count || 0;
    // showData.value[5].data[2].data[0] = cheng6.find((x: any) => x.title === "产出")?.count || 0;
    // showData.value[5].data[3].data[0] = cheng6.find((x: any) => x.title === "投产")?.count || 0;
*/
  } catch (error: any) {
    ElMessage.error(error)
  }
}
onMounted(async () => {
  await getDeptTypes()
  await getData()
  await getData2()
  getEchartsDom()
})

const tableData = ref([])
const getEchartsDom = () => {
  let chartDom = document.getElementById('main')
  let myChart = echarts.init(chartDom)
  let option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    grid: {
      left: 20,
      right: 40,
      top: 30,
      bottom: 80
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: [' '],
      axisLine: {
        show: false // 隐藏 y 轴线
      },
      axisTick: {
        show: false // 隐藏 y 轴刻度
      }
    },
    series: showData.value[0].data
  }
  option && myChart.setOption(option)

  // 第二张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)
  let option2 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    grid: {
      left: 20,
      right: 40,
      top: 30,
      bottom: 80
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: [' '],
      axisLine: {
        show: false // 隐藏 y 轴线
      },
      axisTick: {
        show: false // 隐藏 y 轴刻度
      }
    },
    series: showData.value[1].data
  }
  option2 && myChart2.setOption(option2)

  // 第三张图
  let chartDom3 = document.getElementById('main3')
  let myChart3 = echarts.init(chartDom3)
  let option3 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '18%',
      top: '5%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: [' '],
      axisLine: {
        show: false // 隐藏 y 轴线
      },
      axisTick: {
        show: false // 隐藏 y 轴刻度
      }
    },
    series: showData.value[2].data
  }
  option3 && myChart3.setOption(option3)

  // 第四张图
  let chartDom4 = document.getElementById('main4')
  let myChart4 = echarts.init(chartDom4)
  let option4 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '18%',
      top: '5%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: [' '],
      axisLine: {
        show: false // 隐藏 y 轴线
      },
      axisTick: {
        show: false // 隐藏 y 轴刻度
      }
    },
    series: showData.value[3].data
  }
  option4 && myChart4.setOption(option4)
  // 第成5张图
  let chartDom51 = document.getElementById('main51')
  let myChart51 = echarts.init(chartDom51)
  let option51 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '18%',
      top: '5%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: [' '],
      axisLine: {
        show: false // 隐藏 y 轴线
      },
      axisTick: {
        show: false // 隐藏 y 轴刻度
      }
    },
    series: showData.value[4].data
  }
  option51 && myChart51.setOption(option51)
  // 外协张图
  // let chartDomw = document.getElementById('mainw')
  // let myChartw = echarts.init(chartDomw)
  // let optionw = {
  //   tooltip: {
  //     trigger: 'axis',
  //     axisPointer: {
  //       type: 'shadow'
  //     }
  //   },
  //   legend: {
  //     top: 'bottom'
  //   },
  //   grid: {
  //     left: '3%',
  //     right: '4%',
  //     bottom: '18%',
  //     top: '5%',
  //     containLabel: true
  //   },
  //   xAxis: {
  //     type: 'value'
  //   },
  //   yAxis: {
  //     type: 'category',
  //     data: [' '],
  //     axisLine: {
  //       show: false // 隐藏 y 轴线
  //     },
  //     axisTick: {
  //       show: false // 隐藏 y 轴刻度
  //     }
  //   },
  //   series: showData.value[5].data
  // }
  // optionw && myChartw.setOption(optionw)

  // 第五张图
  let chartDom5 = document.getElementById('main5')
  let myChart5 = echarts.init(chartDom5)
  let option5 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 70,
      right: 0,
      top: 30,
      bottom: 30
    },
    xAxis: [
      {
        type: 'category',
        data: ['配套库存', '大底库存', '鞋面库存'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 15000,
        // interval: 3000,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        label: {
          show: true,
          position: 'top'
        },
        barWidth: '25%',
        tooltip: {
          valueFormatter: function(value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#88ADFB' },
              { offset: 1, color: '#E3ECFF' }
            ]
          )
        },
        data: getMDData.value
      }
    ]
  }
  option5 && myChart5.setOption(option5)
}

onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }

  One()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  width: 100%;
  height: calc(100vh - 7vh - 40px);
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-bottom: 10px;
    padding: 0 10px 5px;
    overflow: hidden;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
